package com.kehao.leetcode.jzoffer;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/14 17:30
 * https://leetcode.cn/problems/wtcaE1/
 */
public class JZSolution016 {
    public int lengthOfLongestSubstring(String s) {
        if(s==null||s.length()==0) return 0;
        Set<Character> set = new HashSet<>();
        int result = 1;
        int left = 0,right = 1;
        set.add(s.charAt(0));
        while (right<s.length()){
            if(set.contains(s.charAt(right))){
                set.remove(s.charAt(left));
                left++;
            }else {
                set.add(s.charAt(right));
                right++;
                result = Integer.max(result,right-left);
            }
        }
        result = Integer.max(result,right-left);
        return result;
    }

    @Test
    public void test01(){
        String s = "abcabcbb";
        System.out.println(lengthOfLongestSubstring(s));
    }
}
